package com.oversea.action.majortype;

import static com.oversea.util.ParamUtil.isNotEmpty;

import java.util.Date;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.oversea.action.BaseAction;
import com.oversea.annotation.Permission;
import com.oversea.models.Majortype;
import com.oversea.service.MajortypeService;
import com.oversea.util.BeanUtils;
import com.oversea.util.page.Page;
import com.oversea.util.page.PageUtil;
import com.oversea.util.searchcontext.SearchContext;
import com.oversea.util.searchcontext.SearchOption;

@ParentPackage("oversea-default")
@InterceptorRefs( { @InterceptorRef("defaultStack") })
@Namespace("/majortype")
@Permission(level = 3)
public class MajorTypeAction extends BaseAction {

	private static final long serialVersionUID = -8199478948374178740L;

	private MajortypeService majortypeService;

	private Majortype majortype;

	private List<Majortype> majortypeList;

	private Page page;

	private int objId;

	private String careerChoices;
	private String startingSalary;
	private String majorTypeName;
	
	@SuppressWarnings("unchecked")
	public Class getActionClass() {
		return Majortype.class;
	}
	
	private SearchContext getListSearchContext() {
		SearchContext searchContext = new SearchContext();
		if (isNotEmpty(majorTypeName)) {
			searchContext.addOption(new SearchOption("majorTypeName", "%"+majorTypeName+"%",
					SearchOption.Option.like));
		}
		if (isNotEmpty(startingSalary)) {
			searchContext.addOption(new SearchOption("startingSalary", startingSalary,
					SearchOption.Option.eqaul));
		}
		if (isNotEmpty(careerChoices)) {
			searchContext.addOption(new SearchOption("careerChoices", careerChoices,
					SearchOption.Option.eqaul));
		}
		searchContext.setSearchClass(getActionClass());
		return searchContext;
	}

	@Action(value = "toadd", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/majortype/majortype_add.ftl") })
	public String add() {
		return SUCCESS;
	}

	@Action(value = "toedit", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/majortype/majortype_edit.ftl") })
	public String edit() {
		majortype = majortypeService.getObjById(objId);
		return SUCCESS;
	}

	@Action(value = "toview", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/majortype/majortype_view.ftl") })
	public String view() {
		majortype = majortypeService.getObjById(objId);
		return SUCCESS;
	}
	
	@Action(value = "toviewmajor", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/majortype/majortype_view_major.ftl") })
	public String viewMajor() {
		majortype = majortypeService.getObjById(objId);
		return SUCCESS;
	}

	@Action(value = "add", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	public String save() {
		majortype.setCreatedTime(new Date());
		majortype.setModifiedTime(new Date());
		majortypeService.save(majortype);
		return SUCCESS;
	}

	@Action(value = "edit", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	// @Result(name = "success", type = "freemarker", location =
	// "/WEB-INF/content/majortype/majortype_view.ftl"
	// @Result(name = "input", type = "freemarker", location =
	// "/WEB-INF/content/majortype/majortype_edit.ftl")
	public String update() {
		Majortype temp = majortypeService.getObjById(majortype.getId());
		majortype.setCreatedTime(temp.getCreatedTime());
		majortype.setModifiedTime(new Date());
		BeanUtils.copyProperties(temp, majortype);
		majortypeService.update(temp);
		majortype = temp;
		return SUCCESS;
	}

	@Action(value = "list", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/content/majortype/majortype_list.ftl") })
	public String list() {
		Majortype searchMajortype = new Majortype();
		searchMajortype.setCareerChoices(careerChoices);
		searchMajortype.setStartingSalary(startingSalary);
		searchMajortype.setMajorTypeName(majorTypeName);
		majortype = searchMajortype;
		page = PageUtil.createPage(pageSize, currentPage, majortypeService
				.getObjectCount(getListSearchContext()).intValue());
		majortypeList = majortypeService.getObjectList(getListSearchContext(),
				pageSize, currentPage);
		return SUCCESS;
	}

	@Action(value = "del", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/default.ftl") })
	// @Result(name = "success", type = "redirect", location = "list.action")
	public String del() {
		Majortype temp = majortypeService.getObjById(objId);
		majortypeService.delete(temp);
		return SUCCESS;
	}

	public Majortype getMajortype() {
		return majortype;
	}

	public void setMajortype(Majortype majortype) {
		this.majortype = majortype;
	}

	public MajortypeService getMajortypeService() {
		return majortypeService;
	}

	public void setMajortypeService(MajortypeService majortypeService) {
		this.majortypeService = majortypeService;
	}

	public List<Majortype> getMajortypeList() {
		return majortypeList;
	}

	public void setMajortypeList(List<Majortype> majortypeList) {
		this.majortypeList = majortypeList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getCareerChoices() {
		return careerChoices;
	}

	public void setCareerChoices(String careerChoices) {
		this.careerChoices = careerChoices;
	}

	public String getStartingSalary() {
		return startingSalary;
	}

	public void setStartingSalary(String startingSalary) {
		this.startingSalary = startingSalary;
	}

	public String getMajorTypeName() {
		return majorTypeName;
	}

	public void setMajorTypeName(String majorTypeName) {
		this.majorTypeName = majorTypeName;
	}

	public int getObjId() {
		return objId;
	}

	public void setObjId(int objId) {
		this.objId = objId;
	}

}
